package cn.fsw.pethome.org.controller;

import cn.fsw.pethome.annotations.PreAuthorize;
import cn.fsw.pethome.org.domain.Shop;
import cn.fsw.pethome.org.domain.ShopAuditLog;
import cn.fsw.pethome.org.query.ShopQuery;
import cn.fsw.pethome.org.service.IShopService;
import cn.fsw.pethome.util.AjaxResult;
import cn.fsw.pethome.util.BusinessException;
import cn.fsw.pethome.util.ExcelUtil;
import cn.fsw.pethome.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    /*导出*/
    @GetMapping("/export")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.selectAll();
            ExcelUtil.exportExcel(shops, "店铺信息", "店铺名字", Shop.class, "shop.xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /*导入*/
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> list = ExcelUtil.importExcel(file,1,1,Shop.class);
        shopService.batchAdd(list);
        System.out.println("导入数据一共【"+list.size()+"】行");
        list.forEach(System.out::println);

    }

    /*审核通过发送激活信息*/
    @GetMapping("/activation/{id}")
    public String activation(@PathVariable("id") Long id){
        try {
            shopService.activation(id);
            return "恭喜您已成功激活";
        } catch (Exception e) {
            e.printStackTrace();
            return "网络异常";
        }
    }



    /*审核通过*/
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.ajaxResult();
        } catch (BusinessException e) {
            return AjaxResult.error().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    /*审核驳回*/
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.ajaxResult();
        } catch (BusinessException e) {
            return AjaxResult.error().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PostMapping("settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.ajaxResult();
        } catch (BusinessException e) {
            return AjaxResult.error().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.ajaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setSuccess(false).setMsg("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.ajaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setSuccess(false).setMsg("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.selectAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PreAuthorize(name = "店铺列表",value = "shop:list")
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.pageSelevtAll(query);
    }

    /*批量添加*/
    @PatchMapping("/batchAdd")
    public AjaxResult batchAdd(@RequestBody List<Shop> list){
        try {
            shopService.batchAdd(list);
            return AjaxResult.ajaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
